package com.wontube.client.extension.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author: yuechao
 * 
 */
public final class StringUtils {
	private static final Logger logger = LoggerFactory.getLogger(StringUtils.class);
	public static final String EMAIL_PATTEN = "^(?:[\\w]+\\.?[\\w]+)+@(?:[\\w]+\\.?[\\w]+)+\\.[a-zA-Z]{2,4}$";

	/**
	 * 格式化字符串
	 * 
	 * @param format
	 * @param s1
	 * @return
	 */
	public static String format(String format, Object s1) {
		if (format == null) {
			return "";
		}
		
		String argument = s1 == null ? "" : s1.toString();
		return format.replaceFirst("\\{\\}", argument);
	}

	public static String format(String format, Object s1, Object s2) {
		Object[] args = new Object[] {s1, s2};
		return format(format, args);
	}

	public static String format(String format, Object... ss) {
		for (int i = 0; i < ss.length; i++) {
			String s = ss[i] == null ? "" : ss[i].toString();
			format = format.replaceFirst("\\{\\}", s);
		}
		
		StringBuilder sb = new StringBuilder();
		
		char last = ' ';
		int index = 0;
		for (int i = 0; i < format.length() - 1; i++) {
			char ch = format.charAt(i);
			if (ch == '}' && last == '{') {
				String s = ss[index] == null ? "" : ss[index].toString();
				sb.replace(i - 1, i, s);
			} else {
				sb.append(ch);
			}
			last = ch;
		}
		
		return format;
	}
	
	/**
	 * 判断指定字符串是否为空，或者没有任何有效字符
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isNullOrEmpty(String s) {
		if (s == null) {
			return true;
		}

		s = s.trim();
		return s.length() == 0;
	}
	
	public static boolean isEqual(String s1, String s2) {
		if (s1 == null) {
			return s2 == null;
		}
		
		return s1.equals(s2);
	}

	/**
	 * 判断指定字符串是否全部为数字
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isDigit(String s) {
		if (isNullOrEmpty(s)) {
			return false;
		}

		for (int i = s.length() - 1; i >= 0; i--) {
			if (!Character.isDigit(s.charAt(i))) {
				return false;
			}
		}

		return true;
	}

	/**
	 * 转换字符集
	 * 
	 * @param text
	 * @param encodeCharset
	 * @return
	 */
	public static String charset(String text, String encodeCharset) {
		return charset(text, null, encodeCharset);
	}

	/**
	 * 转换字符集
	 * 
	 * @param text
	 * @param decodeCharset
	 * @param encodeCharset
	 * @return
	 */
	public static String charset(String text, String decodeCharset,
			String encodeCharset) {
		try {
			text = new String(decodeCharset == null ? text.getBytes()
					: text.getBytes(decodeCharset), encodeCharset);
		} catch (Exception e) {
			logger.error(e.toString());
		}

		return text;
	}
	
	/**
	 * 验证email
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email){
		if(isNullOrEmpty(email)){
			return false;
		}
		return email.matches(EMAIL_PATTEN);
	}
	
	/**
	 * 
	 * @param str (hh:mm:ss | mm:ss)
	 * @return 不合法返回0
	 */
	public static int toSeconds(String str){
		if( ! isNullOrEmpty(str)){
			String[] pies = str.split(":");
			int hours = 0;
			int mins = 0;
			int seconds = 0;
			if(pies.length == 3){
				hours = NumberUtils.parseInt(pies[0], 0);
				mins = NumberUtils.parseInt(pies[1], 0);
				seconds = NumberUtils.parseInt(pies[2], 0);					
			}else if(pies.length == 2){
				mins = NumberUtils.parseInt(pies[0], 0);
				seconds = NumberUtils.parseInt(pies[1], 0);
			}
			if(hours >= 0 && mins >= 0 && mins < 60 
					&& seconds >= 0 && seconds < 60){
				return hours * 60 * 60 + mins * 60 + seconds;
			}
		}
		return 0;
	}
	
	/**
	 * 获得字符串的大写首字母
	 * @param title
	 * @return
	 */
	public static String getInitial(String title){
		if(isNullOrEmpty(title)){
			return null;
		}
		
		String s = null;
		for (int i = 0; i < title.length(); i++) {
			char ch = title.charAt(i);
			if(isAsciiLetter(ch)){
				s = String.valueOf(ch);
				break;
			}
		}
		if(s != null){
			return s.toUpperCase();
		}
		return null;
	}

	public static boolean isAsciiLetter(char ch){
		int a = ch;
		if(a >= 65 && a <= 90){
			return true;
		}
		
		if(a >= 97 && a <= 122){
			return true;
		}
		
		return false;
	}
}
